﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace HYRElectric.PowerSystem
{
	public partial class DlgPanelSystemDetectByActiveView : System.Windows.Forms.Form
	{
		private void GetAllDistributElecEquips()
		{
			this.m_lstDistributElecEquips.Clear();
			foreach (ElementId item in PanelSystemDetectCommand.GetAllElecEquips(this.doc, this.viewId))
			{
				if (!this.equSet.Contains(item))
				{
					this.m_lstDistributElecEquips.Add(item);
				}
			}
		}

		private void UpdateStatusBar()
		{
			int num = (this.tabControl.SelectedIndex == 0) ? this.equSet.Count : this.m_lstDistributElecEquips.Count;
			this.toolStripStatusLabel.Text = "设备数量：";
			ToolStripStatusLabel toolStripStatusLabel = this.toolStripStatusLabel;
			toolStripStatusLabel.Text += num.ToString();
		}

		public void RefreshTab()
		{
			if (this.equSet.Count == 0)
			{
				this.tabControl.SelectedIndex = 1;
			}
		}

		public DlgPanelSystemDetectByActiveView(UIDocument uid)
		{
			this.InitializeComponent();
			this.doc = uid.Document;
			this.uiDoc = uid;
			this.viewId = (this.doc.ActiveView as ViewPlan).Id;
			this.equSet = PanelSystemDetectCommand.GetAllInvalidEquipment(this.doc, this.viewId);
			PanelSystemDetectCommand.GetAllInvalidPanel(this.equSet, this.doc, this.viewId);
			this.GetAllDistributElecEquips();
		}

		private void DlgPanelSystemDetectByActiveView_Load(object sender, EventArgs e)
		{
			this.m_Hook = new KeyboardHook(base.Handle, WindowHelper.HWndRevit.Handle);
			this.m_Hook.HookStart();
			//base.Icon = Resource.YArchitechIcon;
			this.InitData();
		}

		public void InitData()
		{
			this.equSet = PanelSystemDetectCommand.GetAllInvalidEquipment(this.doc, this.viewId);
			PanelSystemDetectCommand.GetAllInvalidPanel(this.equSet, this.doc, this.viewId);
			this.GetAllDistributElecEquips();
			this.lvwDeviceName.Items.Clear();
			this.lvwDeviceNameDistribut.Items.Clear();
			this.Text = "配电检测—" + this.doc.ActiveView.ViewName;
			foreach (ElementId elementId in this.equSet)
			{
				ListViewItem listViewItem = new ListViewItem();
				Element elementById = this.doc.GetElementById(elementId);
				FamilyInstance familyInstance = elementById as FamilyInstance;
				listViewItem.Text = familyInstance.Symbol.Family.Name + ":" + familyInstance.Name;
				listViewItem.Tag = elementById;
				listViewItem.ImageKey = PanelSystemDetectCommand.GetImageByType(this.doc, elementById);
				this.lvwDeviceName.Items.Add(listViewItem);
			}
			foreach (ElementId elementId2 in this.m_lstDistributElecEquips)
			{
				ListViewItem listViewItem2 = new ListViewItem();
				Element elementById2 = this.doc.GetElementById(elementId2);
				FamilyInstance familyInstance2 = elementById2 as FamilyInstance;
				listViewItem2.Text = familyInstance2.Symbol.Family.Name + ":" + familyInstance2.Name;
				listViewItem2.Tag = elementById2;
				listViewItem2.ImageKey = PanelSystemDetectCommand.GetImageByType(this.doc, elementById2);
				this.lvwDeviceNameDistribut.Items.Add(listViewItem2);
			}
			this.UpdateStatusBar();
			this.RefreshTab();
		}

		private void lvwDeviceName_DoubleClick(object sender, EventArgs e)
		{
			if (this.lvwDeviceName.SelectedIndices.Count > 0)
			{
				Element element = this.lvwDeviceName.SelectedItems[0].Tag as Element;
				Selection selection = this.uiDoc.Selection;
				Common.SetElementVisibility(element, this.doc);
				RevitVersionFuncs.ClearSelection(selection);
				RevitVersionFuncs.AddToSelection(selection, element);
				this.uiDoc.ShowElements(element);
			}
		}

		private void DlgPanelSystemDetectByActiveView_Resize(object sender, EventArgs e)
		{
			this.lvwDeviceName.Columns[0].Width = base.Width;
			this.lvwDeviceNameDistribut.Columns[0].Width = base.Width;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			new SubWindow().AssignHandle(this.lvwDeviceName.Handle);
			new SubWindow().AssignHandle(this.lvwDeviceNameDistribut.Handle);
		}

		private void DlgPanelSystemDetectByActiveView_FormClosed(object sender, FormClosedEventArgs e)
		{
			PanelSystemDetectCommand.dlgViewDetect = null;
			this.m_Hook.HookStop();
		}

		private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateStatusBar();
		}

		private void lvwDeviceNameDistribut_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lvwDeviceNameDistribut.SelectedIndices.Count > 0)
			{
				Element element = this.lvwDeviceNameDistribut.SelectedItems[0].Tag as Element;
				Selection selection = this.uiDoc.Selection;
				Common.SetElementVisibility(element, this.doc);
				RevitVersionFuncs.ClearSelection(selection);
				RevitVersionFuncs.AddToSelection(selection, element);
				this.uiDoc.ShowElements(element);
			}
		}

		private KeyboardHook m_Hook;

		private Document doc;

		private UIDocument uiDoc;

		private ElementId viewId;

		private List<ElementId> equSet = new List<ElementId>();

		private List<ElementId> m_lstDistributElecEquips = new List<ElementId>();
	}
}
